<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\portal\model;


use think\Model;
use think\facade\Db;
use api\user\model\UserScoreLogModel;
use app\portal\model\UserRewardDetailModel;

class UserModel extends Model
{
    /**
     * 模型名称
     * @var string
     */
    protected $name = 'user';

    //模型关联方法
    protected $relationFilter = ['user'];


    /**
     * more 自动转化
     * @param $value
     * @return string
     */
    public function getAvatarAttr($value)
    {
        $value = !empty($value) ? cmf_get_image_url($value) : $value;
        return $value;
    }

    /**
     * 关联 user表
     * @return string 关联数据
     */
    public function user()
    {
        return $this->belongsTo('UserModel', 'user_id')->setEagerlyType(1);
    }
    
    /**
     * 给用户添加余额,会按照比例分配到2个账户
     */
    public function add_score($uid,$score,$desc,$type){
        $mall = cmf_get_option('mall');
        $cfg = isset($mall['exchange_percent'])?$mall['exchange_percent']:0;
        $cfg = 10000-$cfg;
        $score0 = $cfg*$score/10000;
        $coin = $score-$score0;
        $user = $this->where('id',$uid)->find();
//        $this->where('id',$uid)->setInc('score',$score0);
//        $this->where('id',$uid)->setInc('coin',$coin);
        $balance = $user->score+$user->coin+$score;
        $this->where('id',$uid)->update(['balance'=>$balance,
            'score'=>$user->score+$score0,
            'coin'=>$user->coin+$coin
//            'score'=>['exp','score+'.$score0],
//            'coin'=>['exp','coin+'.$coin]
                ]);
        $logM = new UserScoreLogModel();
        $logM->insert(['user_id'=>$uid,'type'=>$type,'score'=>$score0,'coin'=>$coin,'action'=>$desc,'create_time'=> time(),'balance'=>$balance]);
    }
    
    /**
     * 给用户添加余额,纯粹的加余额,不会分配两个账户
     */
    public function addScorePure($uid,$score,$desc,$type){
        $user = $this->where('id',$uid)->find();
        $balance = $user->balance+$score;
        $this->where('id',$uid)->update([
            'score'=>$user->score+$score,
            'balance'=>$balance
                ]);
        $logM = new UserScoreLogModel();
        $logM->insert(['user_id'=>$uid,'type'=>$type,'score'=>$score,'coin'=>0,'action'=>$desc,'create_time'=> time(),'balance'=>$balance]);
    }

    /**
     * 任务奖励写日志
     */
    public function addTaskLog($uid, $score, $desc){
        $userRewardDetailModel = new UserRewardDetailModel();
        $userRewardDetailModel->insert(['user_id'=>$uid, 'reward_count'=>$score, 'reward_type'=>3, 'provide_user_id'=>0, 'create_time'=> time(),'remark'=>$desc]);
    }
}
